<template>
  <view class="match-swiper-container">
    <swiper
      class="match-swiper"
      :indicator-dots="showIndicators"
      :autoplay="autoplay"
      :interval="interval"
      :duration="duration"
      :circular="circular"
      indicator-color="rgba(255,255,255,0.3)"
      indicator-active-color="#00D4AA"
      @change="onSwiperChange"
    >
      <swiper-item v-for="(match, index) in matches" :key="index" class="swiper-item">
        <view class="match-card" @click="onMatchClick(match)">
          <!-- 比赛时间 -->
          <view class="match-time-section">
            <text class="match-time">{{ match.time }}</text>
            <text class="match-date">{{ formatDate(match.matchTime) }}</text>
          </view>
          
          <!-- 比赛对阵信息 -->
          <view class="match-teams-section">
            <view class="team home-team">
              <image 
                class="team-logo" 
                :src="match.homeTeam.logo || defaultLogo" 
                mode="aspectFit"
                @error="onImageError"
              ></image>
              <text class="team-name">{{ match.homeTeam.name }}</text>
            </view>
            
            <view class="vs-section">
              <text class="vs-text">VS</text>
              <view class="match-status" :class="getStatusClass(match.watchInfo)">
                {{ match.watchInfo.text }}
              </view>
            </view>
            
            <view class="team away-team">
              <image 
                class="team-logo" 
                :src="match.awayTeam.logo || defaultLogo" 
                mode="aspectFit"
                @error="onImageError"
              ></image>
              <text class="team-name">{{ match.awayTeam.name }}</text>
            </view>
          </view>
          
          <!-- 比赛地点信息 -->
          <view class="match-venue-section">
            <text class="venue-text">📍 {{ match.venue }} · {{ match.city }}</text>
          </view>
          
          <!-- 比赛状态标签 -->
          <view class="match-status-badge" :class="match.status.statusClass">
            {{ match.status.text }}
          </view>
        </view>
      </swiper-item>
    </swiper>
    
    <!-- 自定义指示器 -->
    <view v-if="showCustomIndicators && matches.length > 1" class="custom-indicators">
      <view 
        v-for="(item, index) in matches" 
        :key="index"
        class="indicator-dot"
        :class="{ active: currentIndex === index }"
        @click="goToSlide(index)"
      ></view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'MatchSwiper',
  props: {
    // 比赛数据
    matches: {
      type: Array,
      default: () => []
    },
    // 是否显示指示器
    showIndicators: {
      type: Boolean,
      default: false
    },
    // 是否显示自定义指示器
    showCustomIndicators: {
      type: Boolean,
      default: true
    },
    // 是否自动播放
    autoplay: {
      type: Boolean,
      default: true
    },
    // 自动播放间隔时间
    interval: {
      type: Number,
      default: 4000
    },
    // 切换动画时长
    duration: {
      type: Number,
      default: 500
    },
    // 是否循环播放
    circular: {
      type: Boolean,
      default: true
    },
    // 默认Logo
    defaultLogo: {
      type: String,
      default: '/static/images/default-team-logo.png'
    }
  },
  data() {
    return {
      currentIndex: 0
    }
  },
  methods: {
    // 轮播图切换事件
    onSwiperChange(e) {
      this.currentIndex = e.detail.current
      this.$emit('change', {
        current: this.currentIndex,
        match: this.matches[this.currentIndex]
      })
    },
    
    // 点击比赛卡片
    onMatchClick(match) {
      this.$emit('match-click', match)
    },
    
    // 跳转到指定页面
    goToSlide(index) {
      this.currentIndex = index
      // 触发轮播图切换
      this.$emit('go-to-slide', index)
    },
    
    // 格式化日期
    formatDate(dateTime) {
      if (!dateTime) return ''
      
      const date = new Date(dateTime)
      const today = new Date()
      const tomorrow = new Date(today)
      tomorrow.setDate(today.getDate() + 1)
      
      // 判断是今天、明天还是具体日期
      if (this.isSameDay(date, today)) {
        return '今天'
      } else if (this.isSameDay(date, tomorrow)) {
        return '明天'
      } else {
        const month = String(date.getMonth() + 1).padStart(2, '0')
        const day = String(date.getDate()).padStart(2, '0')
        return `${month}月${day}日`
      }
    },
    
    // 判断是否为同一天
    isSameDay(date1, date2) {
      return date1.getFullYear() === date2.getFullYear() &&
             date1.getMonth() === date2.getMonth() &&
             date1.getDate() === date2.getDate()
    },
    
    // 获取状态样式类
    getStatusClass(watchInfo) {
      if (!watchInfo) return 'status-default'
      
      switch (watchInfo.type) {
        case 'free':
          return 'status-free'
        case 'reservation':
          return 'status-reservation'
        case 'paid':
          return 'status-paid'
        case 'replay':
          return 'status-replay'
        default:
          return 'status-default'
      }
    },
    
    // 图片加载失败处理
    onImageError(e) {
      // 设置默认图片
      e.target.src = this.defaultLogo
    }
  }
}
</script>

<style lang="scss" scoped>
.match-swiper-container {
  position: relative;
  margin: 0 15px;
}

.match-swiper {
  height: 200px;
  border-radius: 12px;
  overflow: hidden;
}

.swiper-item {
  padding: 0 5px;
}

.match-card {
  position: relative;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  
  // 添加背景装饰
  &::before {
    content: '';
    position: absolute;
    top: -50%;
    right: -50%;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
    pointer-events: none;
  }
}

.match-time-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  
  .match-time {
    font-size: 16px;
    font-weight: bold;
    color: #fff;
  }
  
  .match-date {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.8);
  }
}

.match-teams-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
  
  .team {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    
    .team-logo {
      width: 45px;
      height: 45px;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.2);
      margin-bottom: 8px;
      border: 2px solid rgba(255, 255, 255, 0.3);
    }
    
    .team-name {
      font-size: 12px;
      color: #fff;
      text-align: center;
      font-weight: 500;
      max-width: 80px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
  
  .vs-section {
    flex: 0 0 80px;
    display: flex;
    flex-direction: column;
    align-items: center;
    
    .vs-text {
      font-size: 18px;
      font-weight: bold;
      color: #fff;
      margin-bottom: 8px;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    }
    
    .match-status {
      padding: 4px 12px;
      border-radius: 20px;
      font-size: 12px;
      font-weight: 500;
      border: 1px solid rgba(255, 255, 255, 0.3);
      
      &.status-free {
        background: rgba(76, 175, 80, 0.9);
        color: #fff;
        border-color: #4CAF50;
      }
      
      &.status-reservation {
        background: rgba(255, 152, 0, 0.9);
        color: #fff;
        border-color: #FF9800;
      }
      
      &.status-paid {
        background: rgba(244, 67, 54, 0.9);
        color: #fff;
        border-color: #F44336;
      }
      
      &.status-replay {
        background: rgba(158, 158, 158, 0.9);
        color: #fff;
        border-color: #9E9E9E;
      }
      
      &.status-default {
        background: rgba(255, 255, 255, 0.2);
        color: #fff;
      }
    }
  }
}

.match-venue-section {
  .venue-text {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.9);
    display: flex;
    align-items: center;
  }
}

.match-status-badge {
  position: absolute;
  top: 15px;
  right: 15px;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 10px;
  font-weight: 500;
  
  &.status-upcoming {
    background: rgba(33, 150, 243, 0.9);
    color: #fff;
  }
  
  &.status-live {
    background: rgba(244, 67, 54, 0.9);
    color: #fff;
    animation: pulse 2s infinite;
  }
  
  &.status-finished {
    background: rgba(158, 158, 158, 0.9);
    color: #fff;
  }
}

.custom-indicators {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 15px;
  gap: 8px;
  
  .indicator-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.3);
    transition: all 0.3s ease;
    cursor: pointer;
    
    &.active {
      background: #00D4AA;
      transform: scale(1.2);
    }
  }
}

// 动画效果
@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(244, 67, 54, 0.7);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(244, 67, 54, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(244, 67, 54, 0);
  }
}

// 响应式设计
@media (max-width: 350px) {
  .match-card {
    padding: 15px;
  }
  
  .team-logo {
    width: 35px !important;
    height: 35px !important;
  }
  
  .team-name {
    font-size: 11px !important;
    max-width: 70px !important;
  }
  
  .vs-text {
    font-size: 16px !important;
  }
}
</style>




